/*
 * @Author: lwy 1245883359@qq.com
 * @Date: 2024-06-16 18:06:14
 * @LastEditors: lwy 1245883359@qq.com
 * @LastEditTime: 2024-06-16 18:11:10
 * @FilePath: \sixbird-service-house\src\utils\dingTalk.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios';
/**
 * 钉钉授权扫码免登录
 * @param {*} redirect_ip 回调网址
 * @param {*} redirect_path 回调path
 * @param {*} client_id 审批流类型id
 * @param {*} state 附加字段
 * @param {*} cb 登录成功回调
 */
let fn = null 
export async function getAccessToken({
  domId = '',
  cb = () => { },
  redirect_ip = process.env.NODE_ENV === "production" ? 'http://juyue.liuge006.com' : 'http://127.0.0.1',
  redirect_path = '/financeManager/outSourceRecharge/recharge',
  client_id = 'dingvinznketdhxkourt',
  state = '',

} = {}) {
  fn = cb
  window.DTFrameLogin(
    {
      id: domId,
      width: 300,
      height: 300,
    },
    {
      redirect_uri: encodeURIComponent(`${redirect_ip}${redirect_path}`),
      client_id,
      scope: "openid",
      response_type: "code",
      state,
      prompt: "consent",
    },
    (loginResult) => {
      const { redirectUrl, authCode, state } = loginResult;
      // 这里可以直接进行重定向
      fn && fn({ type: 'success', msg: '授权成功', ...loginResult })
      fn = null
    },
    (errorMsg) => {
      // 这里一般需要展示登录失败的具体原因,可以使用toast等轻提示
      fn && fn({ type: 'error', msg: errorMsg })
      fn = null

    }
  );
}